﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DDay.iCal;
using DDay.iCal.Components;
using DDay.iCal.DataTypes;

using DataTransferObject;
using DataTransferObject.MapUnMapTime;
using DataTransferObject.DiaryAndTrackingTask.SupportClassForDiary;
using Engine.TaskPrioritizeAlgo;
using DataService.LocalFile;
using DataService;


namespace Engine.ScheduleAlgo
{
    public class PragmaticScheduleAlgorithm : IScheduleAlgorithm
    {
        const int DYNAMIC_SCHEDULE = 1;
        const int PRIORITY_ONLY_SCHEDULE = 2;
        const int DUEDATE_ONLY_SCHEDULE = 3;
        const int PRIORITY_THEN_DUEDATE_SCHEDULE = 4;
        const int DUEDATE_THEN_PRIORITY_SCHEDULE = 5;

        public List<Event> doScheduleTaskList(TaskList tasks, FreeTimeList freeTimeList, SystemConfiguration systemConfiguration, iCalendar iCal, ref ScheduleReport report)
        {
            throw new NotImplementedException();
        }

        List<Event> IScheduleAlgorithm.doScheduleTaskList(TaskList tasks, MappedFreeTimeList mappedFreeTimeList, 
                                                            SystemConfiguration systemConfiguration, iCalendar oldICal, 
                                                            ref ScheduleReport report, int objectiveCase, JskeDataService dataService)
        {
            
            #region Build TaskListForSchedule from TaskList in DTO

            List<TaskForSchedule> taskListForSchedule = new List<TaskForSchedule>();
            for (int i = 0; i < tasks.GetTaskList.Count; i++)
            {
                //convert Task to TaskForSchedule DataType
                TaskForSchedule tmp = new TaskForSchedule(tasks.GetTaskList[i]);
                taskListForSchedule.Add(tmp);
            }
            for (int i = 0; i < taskListForSchedule.Count; i++)
            {
                //build list cac cha cua no
                for (int j = 0; j < taskListForSchedule[i].PredecessorIDs.Count; j++)
                {
                    for (int k = 0; k < taskListForSchedule.Count; k++)
                    {
                        if (taskListForSchedule[k].TaskID == taskListForSchedule[i].PredecessorIDs[j])//day la mot cha
                        {
                            taskListForSchedule[i].TaskPredecessors.Add(taskListForSchedule[k]);
                            break;
                        }
                    }
                }
                taskListForSchedule[i].ScheduledStartDate = DateTime.MaxValue;
            }

            #endregion

            #region Tính static weight

            for (int i = 0; i < taskListForSchedule.Count; i++)
            {
                // Dộ quan trọng theo Task chiếm 80%, độ quan trọng theo Project chiến 20%
                taskListForSchedule[i].Weight = ((1.0 - (Double)taskListForSchedule[i].Priority / 10.0)*8.0
                                                      + (1.0 - (Double)(taskListForSchedule[i].ProjectID + 1) / (Double)systemConfiguration.TabProjectData.ListProjectName.Count)*2.0)/10.0;
            
                //taskListForSchedule[i].Weight = (Double)taskListForSchedule[i].Priority / 26
                //                                      + (Double)(taskListForSchedule[i].ProjectID + 1) / (Double)systemConfiguration.TabProjectData.ListProjectName.Count * 100 * 100;
            }

            #endregion

            #region Initialize Essential Variables

            iCalendar iCal = new iCalendar();
            List<Event> result = new List<Event>();
            List<TaskForSchedule> candidateTasks = new List<TaskForSchedule>(); //queue chua cac Task tiem nang
            List<TaskForSchedule> processingQueue = new List<TaskForSchedule>(); // queue chua cac Task đang trên thớt
            List<MappedFreeTimeList> workingHourList = new List<MappedFreeTimeList>(); //chua cac working Hour List da duoc map
            MappedFreeTimeList mergeMappedWorkingHourList = new MappedFreeTimeList(); //cac list da duoc merge
            MappedFreeTimeList scaledWorkingHourList = new MappedFreeTimeList(); //list cac working Hour da duoc enlarge
            MappedFreeTimeList globalList = new MappedFreeTimeList(); //free Time List
            MappedFreeTimeList finalList = new MappedFreeTimeList(); //List sau cung co the dung duoc trong giai thuat schedule            
            Double Offset = 0;

            int countWorkingHourItem = systemConfiguration.TabWorkingHoursData.ListOfWorkingHourItems.Count;
            int remainTasks = taskListForSchedule.Count; //so task chua complete

            #endregion

            #region Build FINAL LIST
            for (int i = 0; i < countWorkingHourItem; i++)
            {
                int j;
                for (j = 0; j < taskListForSchedule.Count; j++)
                {
                    if (systemConfiguration.TabWorkingHoursData.ListOfWorkingHourItems[i].WorkingHourID == taskListForSchedule[j].WorkingHourID)
                    {
                        workingHourList.Add(systemConfiguration.TabWorkingHoursData.ListOfWorkingHourItems[i].WorkTime);
                        break;
                    }
                }

                if (j == taskListForSchedule.Count)
                {
                    workingHourList.Add(null);
                }
            }
            
            globalList = MappingService.shiftFTLBaseOnWKHour(mappedFreeTimeList, ref Offset);
            mergeMappedWorkingHourList = MappingService.mergeWorkingHourList_1week(workingHourList);
            scaledWorkingHourList = MappingService.enlargeWorkingHourListToFTListSize(mergeMappedWorkingHourList, globalList.ListFreeTimeBlock.Last().End);
            finalList = MappingService.intersectLists(scaledWorkingHourList, globalList);
            #endregion

            #region Create TaskListForSchedule's MappedDueDate and MappedReleaseDate
            for (int i = 0; i < remainTasks; i++)
            {
                taskListForSchedule[i].MappedDueDate = taskListForSchedule[i].DueDate.Subtract(globalList.Offset).TotalMinutes + Offset;
                taskListForSchedule[i].MappedReleaseDate = taskListForSchedule[i].ReleaseDate.Subtract(globalList.Offset).TotalMinutes + Offset;
                //Thêm Start On Or After
                taskListForSchedule[i].MappedStartOnOrAfterDate = taskListForSchedule[i].StartOnOrAfterDate.Subtract(globalList.Offset).TotalMinutes + Offset;
            }
            #endregion

            int walkfinalListBlock = 0;
            //Duyệt cho đến khi hết Task hoặc hết Block thì dừng lại
            while (remainTasks > 0 && walkfinalListBlock < finalList.ListFreeTimeBlock.Count)
            {
                #region lay cac task khong co cha hoac cha da complete, bo vao candidateTasks
                for (int i = 0; i < tasks.GetTaskList.Count; i++)
                {
                    //Task da hoan tat, bo qua
                    if (taskListForSchedule[i].Completed)
                    {
                        continue;
                    }
                    //Kiem tra xem task dang xet co nam trong candiatequeue chua, neu co roi thi bo qua.
                    bool isContinue = false;
                    for (int j = 0; j < candidateTasks.Count; j++)
                    {
                        if (taskListForSchedule[i] == candidateTasks[j])
                        {
                            isContinue = true;
                            break;
                        }
                    }
                    if (isContinue)
                    {
                        continue;
                    }
                    //Task khong co cha, bo vao CandidateQueue, kiểm tra xem nó eventStart thế nào
                    if (taskListForSchedule[i].TaskPredecessors.Count == 0)
                    {
                        if (taskListForSchedule[i].MappedStartOnOrAfterDate <= finalList.ListFreeTimeBlock[walkfinalListBlock].Start)//Kiem tra start on or After
                        {
                            candidateTasks.Add(taskListForSchedule[i]);
                        }
                    }
                    else
                    {
                        //neu task co cha, duyet tat ca cha cua no xem da complete thi bo task do vao candidate queue
                        int j;
                        for (j = 0; j < taskListForSchedule[i].TaskPredecessors.Count; j++)
                        {
                            //duyet tat ca cha cua task hien tai

                            //neu co mot cha chưa complete thì break
                            if (taskListForSchedule[i].TaskPredecessors[j].Completed)
                            {

                                continue;
                            }

                            else
                            {
                                break;
                            }

                        }
                        if (j == taskListForSchedule[i].PredecessorIDs.Count)
                        {
                            //j == PredecessorIDs.count: duyet het danh sach cha => tat ca cac cha de complete => đưa vào tasklist
                            if (taskListForSchedule[i].MappedStartOnOrAfterDate <= finalList.ListFreeTimeBlock[walkfinalListBlock].Start)//Kiem tra start on or After
                            {
                                candidateTasks.Add(taskListForSchedule[i]);
                            }
                        }
                    }
                }
                #endregion

                #region duyet tat ca cac block trong finalList va tao processingQueue (con goi la priorityQueue)
                //duyet tat ca cac workingHour co trong block
                for (int j = 0; j < finalList.ListFreeTimeBlock[walkfinalListBlock].WorkingHour.Count; j++)
                {
                    //duyet tat ca cac task co trong candidateTasks
                    for (int k = 0; k < candidateTasks.Count; k++)
                    {
                        if (candidateTasks[k].WorkingHourID == finalList.ListFreeTimeBlock[walkfinalListBlock].WorkingHour[j]
                                    && candidateTasks[k].MappedReleaseDate < finalList.ListFreeTimeBlock[walkfinalListBlock].End)
                        {
                            processingQueue.Add(candidateTasks[k]);
                        }
                    }
                }
                #endregion

                //****==****\\
                #region Duyệt ProcessingQueue và chạy ObjectiveFunction để xếp TKB

                if (processingQueue.Count > 0)
                {
                    MappedFreeTimeBlock tempBlock = finalList.ListFreeTimeBlock[walkfinalListBlock];
                    //true: chắc chắn nhảy qua block kế tiếp trong finalList
                    //false: có thể nhảy hoặc không
                    Boolean resultFromObjectiveFunction = true;
                    #region chọn objective function
                    switch (objectiveCase)
                    {
                        case DYNAMIC_SCHEDULE:
                            #region giải thuật dynamic weight
                            PragmaticPrioritizeTaskListAlgo.setAlphaBeta(0.45, 0.55);
                            resultFromObjectiveFunction = PragmaticPrioritizeTaskListAlgo.createEventByDynamicWeight(ref result, ref processingQueue, systemConfiguration,
                                                                                    ref tempBlock, walkfinalListBlock, finalList, Offset);
                            #endregion
                            break;

                        case PRIORITY_ONLY_SCHEDULE:
                            #region giải thuật theo priority only
                            resultFromObjectiveFunction = PragmaticPrioritizeTaskListAlgo.createEventByPriority(ref result, ref processingQueue, systemConfiguration,
                                                                                    ref tempBlock, finalList, Offset);
                            #endregion
                            break;

                        case DUEDATE_ONLY_SCHEDULE:
                            #region giải thuật theo duedate only
                            resultFromObjectiveFunction = PragmaticPrioritizeTaskListAlgo.createEventByDuedate(ref result, ref processingQueue, systemConfiguration,
                                                                                    ref tempBlock, finalList, Offset);
                            #endregion
                            break;

                        case PRIORITY_THEN_DUEDATE_SCHEDULE:
                            #region giải thuật ưu tiên priority hơn duedate
                            PragmaticPrioritizeTaskListAlgo.setAlphaBeta(0.2, 0.8);
                            resultFromObjectiveFunction = PragmaticPrioritizeTaskListAlgo.createEventByDynamicWeight(ref result, ref processingQueue, systemConfiguration,
                                                                                    ref tempBlock, walkfinalListBlock, finalList, Offset);
                            #endregion
                            break;

                        case DUEDATE_THEN_PRIORITY_SCHEDULE:
                            #region giải thuật ưu tiên duedate hơn priority
                            resultFromObjectiveFunction = PragmaticPrioritizeTaskListAlgo.calculateSubsequenceEvent(ref result, ref processingQueue, systemConfiguration,
                                                                                    ref tempBlock, finalList, Offset);
                            #endregion
                            break;
                    }
                    #endregion

                    #region Hậu xử lý sau khi gọi ObjectiveFunction

                    if (resultFromObjectiveFunction)
                    {
                        walkfinalListBlock++;
                        for (int i = 0; i < candidateTasks.Count; i++)
                        {
                            candidateTasks[i].UnSatisfiedMinimumTimeSplit = false;
                        }
                    }
                    else if (processingQueue.Count == 0)
                    {
                        walkfinalListBlock++;
                        for (int i = 0; i < candidateTasks.Count; i++)
                        {
                            candidateTasks[i].UnSatisfiedMinimumTimeSplit = false;
                        }
                    }

                    #endregion
                }
                else
                {
                    walkfinalListBlock++;
                    for (int i = 0; i < candidateTasks.Count; i++)
                    {
                        candidateTasks[i].UnSatisfiedMinimumTimeSplit = false;
                    }
                }

                #endregion
                //****==****\\
                #region schedule cũ [Cancelled]
                /*
                if (processingQueue.Count > 0)
                {
                    if (finalList.FreeTimeBlockList[walkfinalListBlock].EndDateTime - finalList.FreeTimeBlockList[walkfinalListBlock].StartDateTime > processingQueue[0].RemainTime)
                    {
                        Event newEvent = iCal.Create<Event>();
                        DateTime StartDateTime = new DateTime();
                        DateTime EndDateTime = new DateTime();
                        StartDateTime = finalList.Offset.AddMinutes(finalList.FreeTimeBlockList[walkfinalListBlock].StartDateTime - Offset);
                        EndDateTime = finalList.Offset.AddMinutes(finalList.FreeTimeBlockList[walkfinalListBlock].StartDateTime - Offset + processingQueue[0].RemainTime);
                        newEvent.DTStart = new DDay.iCal.DataTypes.iCalDateTime(StartDateTime);
                        newEvent.DTEnd = new DDay.iCal.DataTypes.iCalDateTime(EndDateTime);
                        newEvent.Summary = processingQueue[0].Name;
                        result.Add(newEvent);

                        processingQueue[0].Completed = true;
                        remainTasks--;
                        finalList.FreeTimeBlockList[walkfinalListBlock].StartDateTime = finalList.FreeTimeBlockList[walkfinalListBlock].StartDateTime + processingQueue[0].RemainTime;
                        processingQueue[0].RemainTime = 0;
                        candidateTasks.Remove(processingQueue[0]);
                    }
                    else if (finalList.FreeTimeBlockList[walkfinalListBlock].EndDateTime - finalList.FreeTimeBlockList[walkfinalListBlock].StartDateTime == processingQueue[0].RemainTime)
                    {
                        Event newEvent = iCal.Create<Event>();
                        DateTime StartDateTime = new DateTime();
                        DateTime EndDateTime = new DateTime();
                        StartDateTime = finalList.Offset.AddMinutes(finalList.FreeTimeBlockList[walkfinalListBlock].StartDateTime - Offset);
                        EndDateTime = finalList.Offset.AddMinutes(finalList.FreeTimeBlockList[walkfinalListBlock].EndDateTime - Offset);
                        newEvent.DTStart = new DDay.iCal.DataTypes.iCalDateTime(StartDateTime);
                        newEvent.DTEnd = new DDay.iCal.DataTypes.iCalDateTime(EndDateTime);
                        newEvent.Summary = processingQueue[0].Name;
                        result.Add(newEvent);

                        processingQueue[0].Completed = true;
                        remainTasks--;
                        processingQueue[0].RemainTime = 0;
                        walkfinalListBlock++;
                        candidateTasks.Remove(processingQueue[0]);
                    }
                    else
                    {
                        Event newEvent = iCal.Create<Event>();
                        DateTime StartDateTime = new DateTime();
                        DateTime EndDateTime = new DateTime();
                        StartDateTime = finalList.Offset.AddMinutes(finalList.FreeTimeBlockList[walkfinalListBlock].StartDateTime - Offset);
                        EndDateTime = finalList.Offset.AddMinutes(finalList.FreeTimeBlockList[walkfinalListBlock].EndDateTime - Offset);
                        newEvent.DTStart = new DDay.iCal.DataTypes.iCalDateTime(StartDateTime);
                        newEvent.DTEnd = new DDay.iCal.DataTypes.iCalDateTime(EndDateTime);
                        newEvent.Summary = processingQueue[0].Name;
                        result.Add(newEvent);

                        processingQueue[0].RemainTime = processingQueue[0].RemainTime - (finalList.FreeTimeBlockList[walkfinalListBlock].EndDateTime - finalList.FreeTimeBlockList[walkfinalListBlock].StartDateTime);
                        walkfinalListBlock++;
                    }
                }
                else
                {
                    walkfinalListBlock++;
                }
                 */
                #endregion

                #region cập nhật RemainTask, xóa ProcessingQueue

                foreach (TaskForSchedule tempTask in processingQueue)
                {
                    if (tempTask.Completed)
                    {
                        candidateTasks.Remove(tempTask);
                        remainTasks--;
                    }
                }
                //emtpy processingQueue
                processingQueue.Clear();

                #endregion
            }

            #region Nối các Event kề nhau của cùng một Task lại chung; sau đó tách các event kéo dài trong 2 ngày ra làm 2 event riêng lẻ

            for (int i = 0; i < result.Count - 1; i++)
            {
                if (result[i].DTEnd.Value == result[i + 1].DTStart.Value && result[i].Summary.Value == result[i + 1].Summary.Value)
                {
                    result[i].DTEnd = result[i + 1].DTEnd;
                    result.RemoveAt(i + 1);
                    i--;
                }
            }

            for (int i = 0; i < result.Count; i++)
            {
                if (result[i].DTStart.Day != result[i].DTEnd.AddMinutes(-1).Day)
                {
                    Event newEvent = iCal.Create<Event>();
                    newEvent = result[i].Copy();
                    newEvent.DTStart = new DDay.iCal.DataTypes.iCalDateTime(result[i].DTStart.AddDays(1).Year, result[i].DTStart.AddDays(1).Month, result[i].DTStart.AddDays(1).Day, 0, 0, 0);
                    result.Insert(i + 1, newEvent);

                    result[i].DTEnd = newEvent.DTStart;
                }
            }

            for (int i = 0; i < result.Count; i++)
            {
                result[i].Summary = "[" + result[i].Summary + "] - [" +
                                        (result[i].DTEnd - result[i].DTStart).TotalHours.ToString("F") + "h] [start " +
                                        result[i].DTStart.Hour + ":" + result[i].DTStart.Minute + "] - [end " +
                                        result[i].DTEnd.Hour + ":" + result[i].DTEnd.Minute + "]";
            }


            #endregion

            #region tạo task report
            int id;
            int timeSpan, day, hour, minute;
            String dayString, hourString, minuteString;

            String name, taskProject, workingTime, taskLate, constraintViolation, suggestion;
            DateTime start, end, dueDate;


            for (int i = 0; i < taskListForSchedule.Count; i++)
            {
                taskLate = "";
                constraintViolation = "";
                suggestion = "";
                id = taskListForSchedule[i].TaskID;
                name = taskListForSchedule[i].Name;
                taskProject = systemConfiguration.TabProjectData.ListProjectName[taskListForSchedule[i].ProjectID];
                timeSpan = tasks.GetTaskList[i].RemainTime;
                minute = timeSpan % 60;
                hour = (timeSpan / 60) % 24;
                day = timeSpan / 60 / 24;
                dayString = "";
                hourString = "";
                minuteString = "";
                if (day != 0)
                {
                    dayString = Convert.ToString(day) + "Day(s)";
                }
                if (hour != 0)
                {
                    hourString = Convert.ToString(hour) + "Hour(s)";
                }
                if (minute != 0)
                {
                    minuteString = Convert.ToString(minute) + "Minute(s)";
                }
                workingTime = dayString + hourString + minuteString;

                if (taskListForSchedule[i].ScheduledEndDate > taskListForSchedule[i].DueDate)
                {
                    timeSpan = (int)taskListForSchedule[i].ScheduledEndDate.Subtract(taskListForSchedule[i].DueDate).TotalMinutes;
                    minute = timeSpan % 60;
                    hour = (timeSpan / 60) % 24;
                    day = timeSpan / 60 / 24;
                    dayString = "";
                    hourString = "";
                    minuteString = "";
                    if (day != 0)
                    {
                        dayString = Convert.ToString(day) + "Day(s)";
                    }
                    if (hour != 0)
                    {
                        hourString = Convert.ToString(hour) + "Hour(s)";
                    }
                    if (minute != 0)
                    {
                        minuteString = Convert.ToString(minute) + "Minute(s)";
                    }
                    taskLate = dayString + hourString + minuteString;

                    //taskLate = Convert.ToString(taskListForSchedule[i].ScheduledEndDate.Subtract(taskListForSchedule[i].DueDate).Minutes);
                    constraintViolation = "Due date missed";
                    suggestion = "Please edit due date or total work of this task";

                }

                if (!taskListForSchedule[i].Completed)
                {
                    constraintViolation += "\nDo not have enough time to schedule this task";
                    suggestion += "\nPlease edit your total work of this task";
                    if (taskListForSchedule[i].MinimumTimeSplit > 0)
                    {
                        constraintViolation += "\nMinimum time split may be violated";
                        suggestion = "\nMinimum time split of this task may be too large to find a period of time enough for this task to complete";
                    }
                }

                start = taskListForSchedule[i].ScheduledStartDate;
                end = taskListForSchedule[i].ScheduledEndDate;
                dueDate = taskListForSchedule[i].DueDate;
                String dueDateString = "Unspecified due date";
                if (dueDate < DateTime.MaxValue)
                {
                    dueDateString = Convert.ToString(dueDate);
                }
                report.insertTaskReport(id, name, taskProject, start, end, dueDateString, workingTime, taskLate, constraintViolation, suggestion);
            }


            #endregion

            #region tạo summary report
            String project, workRemaining, late;
            int numberOfTask;

            int projectCount = systemConfiguration.TabProjectData.ListProjectName.Count;
            int[] projectLate = new int[projectCount];
            int[] projectWorkTime = new int[projectCount];
            int[] projectTaskNumber = new int[projectCount];

            for (int i = 0; i < projectCount; i++)
            {
                projectLate[i] = 0;
                projectWorkTime[i] = 0;
                projectTaskNumber[i] = 0;
            }

            for (int i = 0; i < taskListForSchedule.Count; i++)
            {
                if (taskListForSchedule[i].ScheduledEndDate > taskListForSchedule[i].DueDate)
                {
                    if(projectLate[taskListForSchedule[i].ProjectID] < (int)taskListForSchedule[i].ScheduledEndDate.Subtract(taskListForSchedule[i].DueDate).TotalMinutes)
                        projectLate[taskListForSchedule[i].ProjectID] = (int)taskListForSchedule[i].ScheduledEndDate.Subtract(taskListForSchedule[i].DueDate).TotalMinutes;
                }
                projectWorkTime[taskListForSchedule[i].ProjectID] += tasks.GetTaskList[i].RemainTime;
                projectTaskNumber[taskListForSchedule[i].ProjectID]++;
            }

            for (int i = 0; i < projectCount; i++)
            {
                project = systemConfiguration.TabProjectData.ListProjectName[i];

                timeSpan = projectWorkTime[i];
                minute = timeSpan % 60;
                hour = (timeSpan / 60) % 24;
                day = timeSpan / 60 / 24;
                dayString = "";
                hourString = "";
                minuteString = "";
                if (day != 0)
                {
                    dayString = Convert.ToString(day) + "Day(s)";
                }
                if (hour != 0)
                {
                    hourString = Convert.ToString(hour) + "Hour(s)";
                }
                if (minute != 0)
                {
                    minuteString = Convert.ToString(minute) + "Minute(s)";
                }
                workRemaining = dayString + hourString + minuteString;

                timeSpan = projectLate[i];
                minute = timeSpan % 60;
                hour = (timeSpan / 60) % 24;
                day = timeSpan / 60 / 24;
                dayString = "";
                hourString = "";
                minuteString = "";
                if (day != 0)
                {
                    dayString = Convert.ToString(day) + "Day(s)";
                }
                if (hour != 0)
                {
                    hourString = Convert.ToString(hour) + "Hour(s)";
                }
                if (minute != 0)
                {
                    minuteString = Convert.ToString(minute) + "Minute(s)";
                }
                late = dayString + hourString + minuteString;

                numberOfTask = projectTaskNumber[i];
                report.insertSummaryReport(project, numberOfTask, workRemaining, late);
            }

            dataService.dbWorker.writeTaskList(tasks);
            dataService.dbWorker.writeSchedReport(report);

            #endregion

            #region Create & Save ListResult to BinaryFile

            ListEvent listEvtSched = new ListEvent();
            //listEvtSched.ListSchedEvent = result;

            #region Convert Event to JskeEvent DataType
            //evt.AddParameter("taskID", Convert.ToString(t.TaskID));
            //evt.AddParameter("taskName", t.Name);
            //evt.AddParameter("taskProject", systemConfig.TabProjectData.ListProjectName[t.ProjectID]);
            //evt.AddParameter("workingHourName",
            //    systemConfig.TabWorkingHoursData.ListOfWorkingHourItems[t.WorkingHourID].WorkingHourName);
            //evt.AddParameter("taskPriority", Convert.ToString(t.Priority));
            int getHashCode = DateTime.Now.GetHashCode();
            for (int i = 0; i < result.Count; i++)
            {
                JskeEvent jskeEvt = new JskeEvent();

                jskeEvt.EventType = "[Task]";
                jskeEvt.EventName = result[i].Parameters["taskName"].Values[0];
                jskeEvt.EventDuration = (int)(result[i].DTEnd - result[i].DTStart).TotalMinutes;
                
                EventPriority tempPriority;
                if (1 <= int.Parse(result[i].Parameters["taskPriority"].Values[0]) && int.Parse(result[i].Parameters["taskPriority"].Values[0]) <= 3)
                {
                    tempPriority = EventPriority.A;
                }
                else if (int.Parse(result[i].Parameters["taskPriority"].Values[0]) <= 7)
                {
                    tempPriority = EventPriority.B;
                }
                else
                {
                    tempPriority = EventPriority.C;
                }
                                
                jskeEvt.EventPriority = tempPriority;
                jskeEvt.EventStart = result[i].DTStart.Value;
                jskeEvt.TaskPriority = int.Parse(result[i].Parameters["taskPriority"].Values[0]);
                jskeEvt.ProjectName = result[i].Parameters["taskProject"].Values[0];
                jskeEvt.TaskID = int.Parse(result[i].Parameters["taskID"].Values[0]);
                jskeEvt.TaskName = result[i].Parameters["taskName"].Values[0];
                jskeEvt.WorkingHourName = result[i].Parameters["workingHourName"].Values[0];
                jskeEvt.EventID = getHashCode + i;
                listEvtSched.ListSchedJskeEvt.Add(jskeEvt);
            }
            #endregion

            dataService.dbWorker.WriteListEvent(listEvtSched);
            dataService.dbWorker.WriteListSchedEvent(listEvtSched);

            #endregion

            return result;
        }
    }
}
